import React from 'react'
import { connect } from 'dva'
import { Select } from 'antd'
const Option = Select.Option;


/**
 * [TraderSelect 交易员选择框]
 */
class TraderSelect extends React.Component {
  state = {
    data: this.props.traderDefinitionList,
    value: this.props.trader,
  }
  componentDidMount() {
    // this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    console.log(1112233,nextProps,this.props)
    if(nextProps.traderDefinitionList!==this.props.traderDefinitionList){
      const data = nextProps.traderDefinitionList
      data.unshift({
        name: "团队",
        id: "total"
      })
      this.setState({
        data,
      })
    }
  }

  fetchData=(props)=>{
    props.dispatch({
      type: `dailyReplay/getTraderStockDefinitionList`,
      payload:props
    })
  }
  
  handleChange = (value) => {
    console.log(11122,value)
    this.setState({
      value,
    })
    this.changeRouter(value)
  }
  changeRouter(traderVal) {
    //  保存查询参数到URL
    const payload=this.props.location
    payload.query.trader=traderVal
    this.props.dispatch({
      type: `app/changeRouter`,
      payload
    })
    this.props.dispatch({
      type: `dailyReplay/getTSDefinitionList`,
      payload
    })
  }
  render() {
    
    const { value, data } = this.state
    return (
      <Select
        showSearch
        style={{ width: 120 }}
        value={value}
        optionFilterProp="children"
        onChange={this.handleChange.bind(this)}
        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
      >
        {
          data.map((item, index) => <Option value={item.id} key={index}>{item.name}</Option>)
        }
      </Select>
    )
  }
}
TraderSelect.defaultProps={
  //开始时间 => begindate
  begindate:null, 
  //结束时间 => enddate
  enddate:null, 
  //交易员列表 => traderDefinitionList
  traderDefinitionList:[], 
  //当前交易员ID => trader
  trader:'total', 
  //当前路由 => location
  location:{
    pathname:'/tradeReplay/dailyReplay',
    query:{},
  },
}

export default connect()(TraderSelect)
